/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package koor.persistent;

import java.util.List;
import koor.entity.User;
import koor.service.ServiceLocator;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Admin
 */
public class UserDAO extends AbstractDAO<User> {

    public UserDAO() {
        super();
        setClazz(User.class);
    }

    public int checkAuthorisation(String login, String password) {
        User user = findByCriteria(login, "login").get(0);
        return (user.getPassword().equalsIgnoreCase(password)) ? 1 : 0;
    }

    public boolean isActionAllowed(User user, String actionName) {
        return user.getRole().getActions().contains(ServiceLocator.getService(ActionDAO.class).findByName(actionName).get(0));
    }

    @Override
    public List<User> findByName(String string) {
        Session session = null;
        List<User> entityList = null;
        try {
            session = getSession();
            entityList = (List<User>) session.createCriteria(clazz).createCriteria("login").add(Restrictions.eq("login", string)).list();
        } catch (Exception e) {
            //
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return entityList;
    }
}
